package com.bstek.uflo.form.view.common.impl;

import java.util.Map;

import org.htmlparser.nodes.TagNode;
import org.htmlparser.tags.Div;
import org.springframework.util.StringUtils;

import com.bstek.dorado.view.View;
import com.bstek.dorado.view.widget.Component;
import com.bstek.dorado.view.widget.form.TextArea;
import com.bstek.uflo.form.Constants;
import com.bstek.uflo.form.model.TableDefinition;
import com.bstek.uflo.form.view.common.AbstractComponentBuilder;

/**
 * @author Jacky.gao
 * @since 2013年11月6日
 */
@org.springframework.stereotype.Component("com.bstek.uflo.form.view.common.impl.TextAreaEditorBuilder")
public class TextAreaEditorBuilder extends AbstractComponentBuilder {
	public boolean support(TagNode node) {
		if(node instanceof Div){
			Div div=(Div)node;
			String css=div.getAttribute("class");
			if(css!=null && css.equals("uflotextarea")){
				return true;
			}
		}
		return false;
	}

	public void build(TagNode node, View view, Map<String, Component> map,Map<String,Boolean> mappingMap,TableDefinition table) {
		Div element=(Div)node;
		String field=element.getAttribute("bindingfield");
		if(StringUtils.isEmpty(field))return;
		String id=element.getAttribute("id");
		if(map.containsKey(id))return;
		TextArea editor=new TextArea();
		editor.setId(id);
		editor.setRenderOn("#"+id);
		//editor.setWidth(getWidth(element));
		String height=getHeight(element);
		if(height.endsWith("px"))height=height.substring(0,height.indexOf("px"));
		editor.setHeight(height);
		editor.setProperty(field.toLowerCase());
		Boolean hasMapping=mappingMap.get(field);
		if(hasMapping!=null && hasMapping){
			editor.setTrigger("autoMappingDropDown1");			
		}
		editor.setDataSet(Constants.DATASET);
		view.addChild(editor);
		map.put(id, editor);
	}
}
